package com.atguigu.tree;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Queue;

public class BST<E extends Comparable<E>> {

    private class Node {
        public E e;
        public Node left, right;

        public Node(E e) {
            this.e = e;
            this.left = null;
            this.right = null;
        }
    }

    private Node root;
    private int size;

    public BST() {
        root = null;
        size = 0;
    }

    public int Size() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    //向二分搜索树中添加新的元素e
    public void add(E e){
        if(root==null){
            root=new Node(e);
            size++;
        }else{
            add(root,e);
        }
    }
    //向以node为根的二分搜索树中插入元素E，递归算法(先序遍历思想)
    private void add(Node root,E e){
        if(root.e.equals(e)){
            return;
        }else if(root.e.compareTo(e)>0&&root.left==null){
            root.left=new Node(e);
            size++;
            return;
        }else if(root.e.compareTo(e)<0&&root.right==null){
            root.right=new Node(e);
            size++;
            return;
        }
        if(root.e.compareTo(e)>0){
            add(root.left,e);
        }else{
            add(root.right,e);
        }
    }
    //向二分搜索树中添加新的元素e
//    public void add(E e) {
//        root = add(root, e);
//    }
//
//    private Node add(Node root, E e) {
//        if (root == null) {
//            size++;
//            return new Node(e);
//        }
//        if (e.compareTo(root.e) < 0) {
//            root.left = add(root.left, e);
//        } else if(e.compareTo(root.e)>0) {
//            root.right = add(root.right, e);
//        }
//        return root;
//    }

    //查看二分搜索树中是否包含元素e
    public boolean contains(E e) {
        return contains(root, e);
    }

    private boolean contains(Node node, E e) {
        if (root == null) {
            return false;
        }
        if (e.compareTo(node.e) == 0) {
            return true;
        } else if (e.compareTo(node.e) > 0) {
            return contains(node.right, e);
        } else {
            return contains(node.left, e);
        }
    }

    //二分搜索树的前序遍历
    public void preOrder() {
        preOrder(root);
    }

    //前序遍历以node为根的二分搜索树，递归算法
    private void preOrder(Node node) {
        if (node == null) {
            return;
        }
        System.out.println(node.e);
        preOrder(node.left);
        preOrder(node.right);
    }

    //二分搜索树的前序非递归实现
    public void preOrderNR() {
        Deque<Node> stack = new ArrayDeque<>();
        stack.addLast(root);
        while (!stack.isEmpty()) {
            Node node = stack.removeLast();
            System.out.println(node.e);
            if (node.right != null) {
                stack.addLast(node.right);
            }
            if (node.left != null) {
                stack.addLast(node.left);
            }
        }
    }

    //二分搜索树的中序遍历
    public void inOrder() {
        inOrder(root);
    }

    //中序遍历以node为根的二分搜索树，递归算法
    private void inOrder(Node node) {
        if (node == null) {
            return;
        }
        inOrder(node.left);
        System.out.println(node.e);
        inOrder(node.right);
    }

    //二分搜索树的中序遍历非递归实现
    public void inOrderNR() {
        Deque<Node> stack = new ArrayDeque<>();
        Node p = root;
        while (!stack.isEmpty() || p != null) {
            while (p != null) {
                stack.addLast(p);
                p = p.left;
            }
            if (!stack.isEmpty()) {
                p = stack.removeLast();
                System.out.println(p.e);
                p = p.right;
            }
        }
    }

    //二分搜索树的后序遍历
    public void postOrder() {
        postOrder(root);
    }

    private void postOrder(Node node) {
        if (node == null) {
            return;
        }
        postOrder(node.left);
        postOrder(node.right);
        System.out.println(node.e);
    }

    //二分搜索树的层序遍历
    public void levelOrder() {
        Queue<Node> queue = new ArrayDeque<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            Node cur = queue.poll();
            System.out.println(cur.e);
            if (cur.left != null) {
                queue.offer(cur.left);
            }
            if (cur.right != null) {
                queue.offer(cur.right);
            }
        }
    }
    //寻找二分搜索树的最小元素
    public E minimum(){
        if(size==0){
            throw new IllegalArgumentException("BST is empty!");
        }
        return minimum(root).e;
    }
    //返回以root为根的二分搜索树的最小值所在的节点
    private Node minimum(Node root){
        if(root.left==null){
            return root;
        }
        return minimum(root.left);
    }
//寻找二分搜索树的最大元素
    public E maximum(){
        if(size==0){
            throw new IllegalArgumentException("BST is empty!");
        }
        return maximum(root).e;
    }
    //返回以root为根的二分搜索树的最大值所在的节点
    private Node maximum(Node root){
        if(root.right==null){
            return root;
        }
        return maximum(root.right);
    }
    //从二分搜索树中删除最小值所在的节点，返回最小值
    public E removeMin(){
        E target=minimum();
        root=removeMin(root);
        return target;
    }
    //删除掉以root为根的二分搜索树中的最小节点，返回删除节点后新的二分搜索树的根
    private Node removeMin(Node root){
        if(root.left==null){
            Node rightNode=root.right;
            root.right=null;
            size--;
            return rightNode;
        }
        root.left=removeMin(root.left);
        return root;
    }
    //从二分搜索树中删除最大值所在的节点，返回最大值
    public E removeMax(){
        E target=maximum();
        root=removeMax(root);
        return target;
    }
    //删除掉以root为根的二分搜索树中的最大节点，返回删除节点后新的二分搜索树的根
    private Node removeMax(Node root){
        if(root.right==null){
            Node leftNode=root.left;
            root.left=null;
            size--;
            return leftNode;
        }
        root.right=removeMax(root.right);
        return root;
    }
    //删除任意节点
    public void remove(E e){
        root=remove(root,e);
    }
    //删除以node为根的二分搜索树中值位e的节点，递归算法
    //返回删除节点后新的二分搜索树的根
    private Node remove(Node node,E e){
        if(node==null){
            return null;
        }
        if(e.compareTo(node.e)<0){
            node.left=remove(node.left,e);
        }else if(e.compareTo(node.e)>0){
            node.right=remove(node.right,e);
        }else{
            //左子树为空
            if(node.left==null){
                Node rightNode=node.right;
                node.right=null;
                size--;
                return rightNode;
            }
            //右子树为空
            if(node.right==null){
                Node leftNode=node.left;
                node.left=null;
                size--;
                return leftNode;
            }
            //待删除节点左右子树均不为空
            //找到比待删除节点大的最小节点，即待删除节点右子树的最小节点
            //用这个节点顶替待删除节点的位置
            Node successor=minimum(node.right);
            successor.right=removeMin(node.right);
            successor.left=node.left;
            node.left=node.right=null;
            return successor;
        }
        return node;
    }

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        generateBSTString(root, 0, res);
        return res.toString();
    }

    //生成以node为根节点，深度为depth的描述二叉树的字符串
    private void generateBSTString(Node node, int depth, StringBuilder res) {
        if (node == null) {
            res.append(generateDepthString(depth)).append("null\n");
            return;
        }
        res.append(generateDepthString(depth)).append(node.e).append("\n");
        generateBSTString(node.left, depth + 1, res);
        generateBSTString(node.right, depth + 1, res);
    }

    private String generateDepthString(int depth) {
        StringBuilder res = new StringBuilder();
        for (int i = 0; i < depth; i++) {
            res.append("--");
        }
        return res.toString();
    }


}
